// pages/bindBank/bindBank.js
import {businessWalletInfoApi,GetSendCaptchaApi,GetBankInfoApi,GetBindBankCardApi} from '../../utils/api.js';

import { GetBankCardInfoApi,GetStorewalletinfoApi,storebankcardsendApi,storebankcardbindApi } from '../../utils/newApi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    mobile:'',//手机号
    inputCode:'',//验证码
    sendFlag:false,//是否发送
    phoneFlag:true,//手机号是真实的
    TimeIntval:60,

    userInfo:{},
    bankCard:'',//银行卡号6217007200085193327
    bankInfo:{},//银行信息
    accountName:''
  },
  //获取钱包信息
  GetWalletInfo(){
    GetStorewalletinfoApi({storeId: this.data.id}).then(res=>{
      this.setData({
        userInfo:res.data,
        // bankCard:res.data.bankNumber.substr(-4)
      })
    })
  },
  // 填写银行卡号
  getBankCard(e){
    this.setData({
      bankCard : e.detail.value
    })
    let reg = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/
    console.log(reg.test(this.data.bankCard),"q111")
    if(reg.test(this.data.bankCard)){
      console.log(this.data.bankCard,"this.data.bankCard")
      this.GetBankInfo();
    }
  },
  // 获取开户名
  getAccountName(e){
    this.setData({
      accountName : e.detail.value
    })
  },
  // 获取银行信息
  GetBankInfo(){
    GetBankCardInfoApi({bank_card_no:this.data.bankCard}).then(res=>{
      this.setData({
        bankInfo:res.data.data[0]
      })
    })
  },
  // 立即绑定
  immediatelyBind(){

    if(!this.data.bankCard){
      wx.showToast({
        title:"请输入银行卡号",
        icon:"none",
        duration:2000
      })
      return;
    }
    if(!this.data.accountName){
      wx.showToast({
        title:"请输入开户名",
        icon:"none",
        duration:2000
      })
      return;
    }


    // if(!this.data.bankCard){
    //   wx.showToast({
    //     title:"请输入正确的银行卡号",
    //     icon:"none",
    //     duration:2000
    //   })
    //   return;
    // }

    if(!this.data.inputCode){
      wx.showToast({
        title:"请输入验证码",
        icon:"none",
        duration:2000
      })
      return;
    }

    let data={
      accountNum:this.data.bankCard,
      accountName:this.data.accountName,
      // bankName:this.data.bankInfo?this.data.bankInfo.bankName:'',
      code:this.data.inputCode,
      // platform:'xiuxian',
      storeId:this.data.id,
      mobile:this.data.userInfo.mobile
    }
    storebankcardbindApi(data).then(res=>{
      if(res.code==0){
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000,
          success: () => {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              });
            }, 500);
          },
        })
      }else{
        wx.showToast({
          title:res.message,
          icon:"none",
          duration:2000
        })
      }
    })
  },

  getMobile(e){
    this.setData({
      mobile : e.detail.value
    })
    if(this.data.mobile.length==11){
      this.phoneExc();
    }
  },
  phoneExc(){
    let that = this;
    if(that.data.mobile){
      let phoneFlag = /^(0|86|17951)?(10[0-9]|11[0-9]|12[0-9]|13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])[0-9]{8}$/.test(that.data.mobile);
      that.setData({
        phoneFlag:phoneFlag
      })
      if(!phoneFlag){
        wx.showToast({
          title: '请输入正确的手机号',
          icon:"none",
          duration:2000
        })
      }
    }
  },
  getCode(e){
    this.setData({
      inputCode : e.detail.value
    })
  },
  // 获取验证码
  sendCode(){
    let that = this;
    let second = that.data.TimeIntval;
    that.setData({
      inputCode:''
    })
    if(that.data.phoneFlag){
        that.setData({
        sendFlag : true
      })
      let data={
        mobile:that.data.userInfo.mobile
      }
      storebankcardsendApi(data).then(res=>{
        let time = setInterval(function(){
          second --;
          that.setData({
            TimeIntval: second
          })
          if(that.data.TimeIntval<=0){
            clearInterval(time);
            that.setData({
              sendFlag : false,
              TimeIntval:60
            })
          }
        },1000);
        that.setData({
          timer : time 
        })
      })
    }else{
      wx.showToast({
        title:"请输入正确手机号",
        icon:"none",
        duration:2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
    })
   this.GetWalletInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})